import 'package:flutter/material.dart';
import '../../api/DictApi.dart';
import '../../utils/Settings.dart';
import '../../utils/StoreData.dart';
import '../../utils/Utils.dart';
import 'package:get/get.dart';
import 'package:cached_network_image/cached_network_image.dart';


class UserIndexPage extends StatefulWidget {
  const UserIndexPage({super.key});

  @override
  State<UserIndexPage> createState() => _UserIndexState();
}

class _UserIndexState extends State<UserIndexPage> with AutomaticKeepAliveClientMixin {

  String dictCodes = "${Settings.dictCodes["组织"]}";

  @override
  void initState() {
    initDict();
    super.initState();
  }

  ///初始化下拉数据
  initDict()async{
    if(!Utils.isEmpty(dictCodes)){
      await DictApi.initDict(dictCodes.toString(), "", []);
      setState(() {});
    }
  }

  Widget buildCircleAvatar(networkImageUrl,localImageUrl){
    return FutureBuilder<bool>(
      future: Utils.checkNetworkImageExists(networkImageUrl), // 检查网络图片是否存在
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const CircularProgressIndicator(); // 加载中的指示器
        } else if (snapshot.hasError || !snapshot.data!) {
          // 如果网络图片不存在或加载失败，则使用本地图片
          return CircleAvatar(
            backgroundColor: Colors.transparent,
            backgroundImage: AssetImage(localImageUrl), // 使用本地图片
            minRadius: 35,
            maxRadius: 40,
          );
        } else {
          // 网络图片存在且加载成功
          return CircleAvatar(
            backgroundColor: Colors.transparent,
            backgroundImage: CachedNetworkImageProvider(networkImageUrl), // 使用网络图片
            minRadius: 35,
            maxRadius: 40,
          );
        }
      },
    );
  }


  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: Settings.backgroundColor,
      appBar: AppBar(
        title: const Text("个人中心"),
        centerTitle: true,
        elevation: 0,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Card(
              
              elevation: 0,
              margin:const EdgeInsets.only(left: 10,right: 10,top: 10),
              child: Row(
                children: [
                  const Center(
                    child: Padding(
                      padding: const EdgeInsets.all(10),
                      // child: buildCircleAvatar('${Settings.fileServer}/${StoreData.sysEmp['profilePicture']}','images/zhongfu.png')
                      child: CircleAvatar(
                        backgroundColor: Colors.transparent,
                        backgroundImage: AssetImage('images/zhongfu.png'), // 使用本地图片
                        minRadius: 35,
                        maxRadius: 40,
                      ),
                    ),
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text("${StoreData.sysEmp['empName']}",style: const TextStyle(fontSize: 20,fontWeight: FontWeight.w600),),
                      const SizedBox(height: 10,),
                      Text(Utils.filterDictText(dictCodes,{},StoreData.sysEmp['orgNo']),style: const TextStyle(fontSize: 14,color: Colors.black54),),
                    ],
                  )
                ],
              ),
            ),
            Card(
              elevation: 0,
              margin:const EdgeInsets.all(10),
              child: Column(
                children: [
                  ListTile(
                    trailing:const Icon(Icons.chevron_right,color: Colors.black38,),
                    leading:const Icon(Icons.person,color: Settings.primaryColor,),
                    title: const Text("个人信息"),
                    onTap: ()async{
                      Get.toNamed("/user/userInfo");
                    },
                  ),
                  // ListTile(
                  //   trailing:const Icon(Icons.chevron_right,color: Colors.black38,),
                  //   leading:const Icon(Icons.settings,color: Settings.primaryColor,),
                  //   title: const Text("个人管理"),
                  //   onTap: ()async{
                  //     Get.toNamed("/user/userSet");
                  //   },
                  // ),
                  // ListTile(
                  //   trailing:const Icon(Icons.chevron_right,color: Colors.black38,),
                  //   leading:const Icon(Icons.priority_high,color: Settings.primaryColor,),
                  //   title: const Text("关于我们"),
                  //   onTap: ()async{
                  //
                  //   },
                  // ),
                  // ListTile(
                  //   trailing:const Icon(Icons.chevron_right,color: Colors.black38,),
                  //   leading:const Icon(Icons.cloud_download,color: Settings.primaryColor,),
                  //   title: const Text("版本更新"),
                  //   onTap: ()async{
                  //     Get.toNamed("/user/userUpdate");
                  //   },
                  // )
                ],
              ),
            )
          ],
        ),
      )
    );
  }


}
